package com.mes.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizCustomerPayableDetail;
import com.mes.biz.domain.vo.ExportStatementDetail;
import com.mes.biz.mapper.BizCustomerPayableDetailMapper;
import com.mes.biz.service.BizCustomerPayableDetailService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.page.PageDomain;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.sql.SqlUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 应收账款明细Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-04-30
 */
@Service
@Transactional(readOnly = true)
public class BizCustomerPayableDetailServiceImpl extends BaseServiceImpl<BizCustomerPayableDetailMapper, BizCustomerPayableDetail> implements BizCustomerPayableDetailService {

    @Resource
    private BizCustomerPayableDetailMapper bizCustomerPayableDetailMapper;

    @Override
    public List<BizCustomerPayableDetail> getBizCustomerPayableDetailByCustomerId(String customerId) {
        return bizCustomerPayableDetailMapper.getBizCustomerPayableDetailByCustomerId(customerId);
    }

    @Override
    public PageInfo<BizCustomerPayableDetail> getByCustomerId(BizCustomerPayableDetail bizCustomerPayableDetail) {
        PageDomain page = bizCustomerPayableDetail.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        if (StringUtils.isNotEmpty(bizCustomerPayableDetail.getOrderStatus())){
            String[] status = bizCustomerPayableDetail.getOrderStatus().split(",");
            bizCustomerPayableDetail.setStatus(status);
        }
        return new PageInfo(bizCustomerPayableDetailMapper.getByCustomerId(bizCustomerPayableDetail));
    }

    @Override
    public List<BizCustomerPayableDetail> getBizCustomerPayableDetailByAccountCode(String accountCode, String type) {
        return bizCustomerPayableDetailMapper.getBizCustomerPayableDetailByAccountCode(accountCode,type);
    }

    @Override
    public boolean deleteByOderProductId(String orderProductId, String orderNumber) {
        return bizCustomerPayableDetailMapper.deleteByOderProductId(orderProductId, orderNumber, BaseEntity.DEL_FLAG_DELETE)>0;
    }

    @Override
    public boolean updateAccountCode(String[] ids) {
        return  bizCustomerPayableDetailMapper.updateAccountCode(ids)>0;
    }

    @Override
    public BizCustomerPayableDetail getInfoByOrderProductId(String orderProductId, String orderNumber) {
        return bizCustomerPayableDetailMapper.getInfoByOrderProductId(orderProductId, orderNumber);
    }

    @Override
    public boolean checkAccountCodeByOrderNumber(String orderNumber) {
        boolean flag=false;
        List<BizCustomerPayableDetail> list = bizCustomerPayableDetailMapper.checkAccountCodeByOrderNumber(orderNumber);
        if (list!=null && list.size()>0){
            for (BizCustomerPayableDetail detail : list) {
                if (StringUtils.isNotEmpty(detail.getAccountCode())){
                    flag=true;
                }
            }

        }
        return flag;
    }

    @Override
    public List<ExportStatementDetail> exportStatement(String[] ids) {
        return bizCustomerPayableDetailMapper.exportStatement(ids);
    }

    @Override
    public BizCustomerPayableDetail getByAfterSaleOrderId(String afterSaleOrderId) {
        return bizCustomerPayableDetailMapper.getByAfterSaleOrderId(afterSaleOrderId);
    }

    @Override
    public Map<String, Object> getIncomeDataByToday() {
        return bizCustomerPayableDetailMapper.getIncomeDataByToday();
    }
}
